package com.example.txerra.src;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.text.method.ScrollingMovementMethod;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.example.txerra.src.Data.Plato;

import org.w3c.dom.Text;

import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

public class Detail extends Activity
{
    private MyApp app;
    private int type;
    private ArrayList<String> nombresPlatos;
    private ListView lvCarta;
    private Plato p;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detail);

        //Bektorea hasieratu
        nombresPlatos = new ArrayList<String>();
        //aplikazio aldagaiak lortzeko
        app = (MyApp)getApplication();
        //Konponenteak lortu
        //ListView-a
        lvCarta = (ListView)this.findViewById(R.id.lvCarta);
        lvCarta.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                //TODO: Klik egitean zer?
                String  itemValue    = (String) lvCarta.getItemAtPosition(position);
                showPlato(getPlato(itemValue));

            }
        });

        //Deskripzioan scrollbar-a jartzeko
        TextView textview= (TextView) findViewById(R.id.tvDescr);
        textview.setMovementMethod(new ScrollingMovementMethod());

        /*b = savedInstanceState;
        type = b.getInt("type");*/
    }

    //Activity hau visible egiten dugun bakoitzean
    //metodo hau erabiliko da. Bundle gabe.... probak egin beharko dira.
    protected void onStart()
    {
        super.onStart();
        Intent myIntent = getIntent();
        int tipo = myIntent.getIntExtra("type",0);
        //bektorea hasieratu(ez bikoizteko)
        nombresPlatos.clear();
        //Listako izenburua
        TextView textView = new TextView(this.getApplicationContext());
        textView.setText(Definitions.mTipos.get(tipo));
        textView.setTextSize(30);

        lvCarta.addHeaderView(textView);
        //datu basetik platerak lortu(Mota 1)
        Cursor c = app.db.rawQuery("SELECT nombre FROM Platos WHERE tipo=?", new String[] {Integer.toString(tipo)});
        if(c.moveToFirst()){
            do{
                //assing values
                nombresPlatos.add(c.getString(0));
            }while(c.moveToNext());
        }
        c.close();
        //lista bete
        String[] values=  nombresPlatos.toArray(new String[0]);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1,values );
        lvCarta.setAdapter(adapter);

        lvCarta.setSelection(0);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.detail, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    private Plato getPlato(String name)
    {
        try {
            Cursor c = app.db.rawQuery("SELECT * FROM Platos WHERE nombre=\"" + name + "\"", new String[]{});
            if (c.moveToFirst()) {
                do {
                    //assing values
                    p = new Plato(c.getString(0), c.getString(1), c.getFloat(2), c.getInt(3));
                } while (c.moveToNext());
            }
            c.close();
        }
        catch(Exception e) {
            e.printStackTrace();
        }
        return p;
    }

    private void showPlato(Plato p)
    {
        TextView tvTitulo = (TextView) findViewById(R.id.tvTitulo);
        TextView tvDescr = (TextView) findViewById(R.id.tvDescr);
        TextView tvPrecio = (TextView) findViewById(R.id.tvPrecio);
        ImageView ivFoto = (ImageView) findViewById(R.id.ivFoto);

        tvTitulo.setText(p.getNombre());
        tvDescr.setText(p.getDescr());
        tvPrecio.setText(String.valueOf(p.getPrecio()));

        String path = Environment.getExternalStorageDirectory().getAbsolutePath();
        File image = new  File(path+"/p1.jpg");
        if(image.exists()) {
            /*Drawable d = Drawable.createFromPath(f.getAbsolutePath());
            ivFoto.setImageDrawable(d);*/
            try{
                String[] s1 = image.list();
                Bitmap bmp = BitmapFactory.decodeStream(new FileInputStream(image.getAbsolutePath()));
                ivFoto.setImageBitmap(bmp);
            }
           catch(Exception e)
           {
               e.printStackTrace();
           }
        }
    }


    public void volver(View view)
    {
        //TODO: Itzuli main-era
        finish();
    }
}
